package com.example.demo.service;

import com.example.demo.domain.Message;
import com.example.demo.domain.repository.MessageRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: demo
 * @description: 系统通知的服务类
 * @author: wangze
 * @create: 2020-04-27 10:18
 **/
@Service
public class MessageService {
    @Autowired
    private MessageRepository messageRepository;

    /**
     * @Description: 根据职工工号查找对应用户的所有消息
     * @Param:  worknumber: 职工工号
     * @return:  消息列表
     * @Author: wangze
     */
    public List<Message> findByWorkNumber(String worknumber){
        return messageRepository.findByWorkNumberOrderByDateDesc(worknumber);
    }

    /**
     * @Description: 获取用户未读信息的总数
     * @Param:  worknumber: 职工工号
     * @return:  未读消息总数
     * @Author: wangze
     */
    public int getTotal(String worknumber){
        return messageRepository.findByWorkNumberAndStateIsFalse(worknumber).size();
    }

    /**
     * @Description: 根据ID查找某个消息
     * @Param:  id: 消息ID
     * @return:  消息实体
     * @Author: wangze
     */
    public Message findMessageById(Integer id){
        return messageRepository.findMessageById(id);
    }

    /**
     * @Description: 设置消息为已读状态
     * @Param:
     * @return:
     * @Author: wangze
     */
    public Message setState(Integer id){
        Message message=messageRepository.findMessageById(id);
        message.setState(true);
        return messageRepository.save(message);
    }
}
